Skip to content

FontChooserButton

Base classes

Name Children Inherits
Widget
prettyqt.widgets.widget
The base class of all user interface objects.

⋔ Inheritance diagram

graph TD
  3043035816["custom_widgets.FontChooserButton"]
  3664736470["widgets.Widget"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  3664736470 --> 3043035816
  829357528 --> 3664736470
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  3620195106 --> 3664736470
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: Widget

Source code in prettyqt/custom_widgets/editors/fontchooserbutton.py
class FontChooserButton(widgets.Widget):
    value_changed = core.Signal(gui.QFont)

    def __init__(
        self,
        font: gui.QFont | str | None = None,
        object_name: str = "font_chooser_button",
        **kwargs,
    ):
        super().__init__(object_name=object_name, **kwargs)
        self._current_font: gui.QFont
        layout = self.set_layout("horizontal", margin=0)
        self.lineedit = widgets.LineEdit(read_only=True)
        self.set_current_font(font)
        layout.add(self.lineedit)
        action = gui.Action(icon="mdi.format-font", triggered=self.choose_font)
        self.button = widgets.ToolButton()
        self.button.setDefaultAction(action)
        layout.add(self.button)

    def __repr__(self):
        return get_repr(self, self.current_font)

    @core.Slot()
    def choose_font(self):
        dlg = widgets.FontDialog()
        if self._current_font:
            dlg.setCurrentFont(self._current_font)

        if dlg.exec():
            self.set_current_font(dlg.get_current_font())
            self.value_changed.emit(gui.QFont(dlg.get_current_font()))

    def set_current_font(self, font: str | gui.QFont | None):
        match font:
            case str():
                self._current_font = gui.Font(font)
            case None:
                self._current_font = gui.Font()
            case _:
                self._current_font = font
        self.lineedit.setText(self._current_font.family())

    def set_value(self, value: str | gui.QFont):
        self.set_current_font(value)

    def get_value(self) -> gui.QFont:
        return gui.QFont(self._current_font)

    current_font = core.Property(
        gui.QFont,
        get_value,
        set_value,
        user=True,
        doc="Currently chosen font",
    )

:fa-brands-github: Show source on GitHub